package com.vgooo.client.ui;


import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.vgooo.client.R;
import com.vgooo.util.Utils;


/**
 * 输入框+文字显示+复选按钮+确定取消按钮
 * 
 * 对于有输入框、activity后退弹窗等等，用dialog明显更合适
 * 这里可以比较方便的切换PopupWindow 和 Dialog
 * 
 */
public class EnterWindow extends Dialog {
	private Context context;
	private View parent;
	public EnterView mEnterView;
	int width;
	
	public EnterWindow(Context context) {
		super(context);
		this.context = context;
		Init();
	}

	private void Init() {
		// TODO Auto-generated method stub

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(new BitmapDrawable());

		width = Utils.getWidthPixels(context);

		mEnterView = new EnterView(context);
		mEnterView.setLayoutParams(new LayoutParams((int) (width * 0.55),
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

		// LayoutParams params = new LayoutParams();
		// params.x = -80;//设置x坐标
		// params.y = -60;//设置y坐标
		// setAttributes(params);

		// this.setWidth((int)(width*0.85));
		// this.setHeight(LayoutParams.WRAP_CONTENT);
		// this.setAnimationStyle(android.R.style.Animation_Activity);
		// this.update();
		// this.setBackgroundDrawable(context.getResources().getDrawable(
		// R.drawable.transblock));
		// this.setFocusable(true);
		this.setContentView(mEnterView, new LayoutParams((int) (width * 0.85),
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
		// this.update();

	}

	// @Override
	// public void dismiss()
	// {
	// // TODO Auto-generated method stub
	// super.dismiss();
	// mEnterView.removeAllViews();
	// mEnterView = null;
	// System.gc();
	// }

	public void addTitle(String mText) {
		mEnterView.addTitle(mText);
	}

	public void addEditText(String mHeader, String mHint, String mText) {
		mEnterView.addEditText(mHeader, mHint, mText);
	}
	public void addEditText( String mText) {
		mEnterView.addEditText(mText);
	}

	public void addTextView(String mText) {
		mEnterView.addTextView(mText);
	}

	public void addCheckBox(String mText) {
		mEnterView.addCheckBox(mText,false,null);
	}
	public void addCheckBox(String mText,boolean check,String tvText) {
		mEnterView.addCheckBox(mText,check,tvText);
	}

	public void addRadioGroup(String[] group) {
		mEnterView.addRadioGroup(group);
	}

	public void addEnterButton(String[] buttons, EnterClickListener mListener) {
		mEnterView.addEnterButton(buttons, mListener);
	}
	
	
	
	/**
	 * Added by LiYongNam 2012.12.13_start
	 */
	
	public interface EnterClickListener_1 {
		void onClick(ArrayList<String> editText, ArrayList<Boolean> isCheck,
				ArrayList<Integer> radioGroup, ArrayList<String> numberPicker, int tag);
	}
	
	public void addNumberPicker( String[] items ){
		mEnterView.addNumberPicker( items );
	}
	
	public void addEnterButton_1(String[] buttons, EnterClickListener_1 mListener) {
		mEnterView.addEnterButton_1(buttons, mListener);
	}	
	
	
	
	/**
	 * Added by LiYongNam 2012.12.13_end
	 */
	

	/**
	 * 得到添加的组件EditText
	 */
	public ArrayList<EditText> getEditText() {
		int count = mEnterView.mContent.getChildCount();
		ArrayList<EditText> mET = new ArrayList<EditText>();
		for (int i = 0; i < count; i++) {
			Object obj = mEnterView.mContent.getChildAt(i).getTag();
			if (obj != null && obj.toString().equals("EditText")) {
				LinearLayout mLayout = (LinearLayout) mEnterView.mContent
						.getChildAt(i);
				EditText mEditText = (EditText) mLayout.getChildAt(1);
				mET.add(mEditText);
			}
		}
		return mET;
	}

	/**
	 * 得到添加的组件TextView
	 */
	public ArrayList<TextView> getTextView() {
		int count = mEnterView.mContent.getChildCount();
		ArrayList<TextView> mEv = new ArrayList<TextView>();
		for (int i = 0; i < count; i++) {
			Object obj = mEnterView.mContent.getChildAt(i).getTag();
			if (obj != null && obj.toString().equals("TextView")) {
				TextView mTextView = (TextView) mEnterView.mContent
						.getChildAt(i);
				mEv.add(mTextView);
			}
		}
		return mEv;
	}

	public void show() {
		super.show();
		// if(parent == null)
		// {
		// this.showAtLocation(
		// MusicApplication.getInstance().getMainAct().findViewById(R.id.main),
		// Gravity.CENTER, 0, 0);
		// }else
		// {
		// this.showAtLocation(
		// parent, Gravity.CENTER, 0, 0);
		// }

	}

	public void setParentView(View parent) {
		this.parent = parent;
	}

	public interface EnterClickListener {
		void onClick(ArrayList<String> editText, ArrayList<Boolean> isCheck,
				ArrayList<Integer> radioGroup, int tag);
	}	

	class EnterView extends LinearLayout {

		/**
		 * 标题
		 */
		LinearLayout mTop;

		/**
		 * 内容
		 */
		LinearLayout mContent;

		public EnterView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			Init();
		}

		private void Init() {
			// TODO Auto-generated method stub
			this.setOrientation(LinearLayout.VERTICAL);

			mTop = new LinearLayout(getContext());
			mTop.setGravity(Gravity.CENTER);
			mTop.setBackgroundResource(R.drawable.bg_transparent);
			mTop.setPadding(Utils.dipTopx(context, 15), 0, Utils.dipTopx(context, 15),0);

			mContent = new LinearLayout(getContext());
			mContent.setBackgroundResource(R.drawable.bg_transparent);
			mContent.setPadding(Utils.dipTopx(getContext(), 13),
					Utils.dipTopx(getContext(), 13),
					Utils.dipTopx(getContext(), 13),
					Utils.dipTopx(getContext(), 13));
			mContent.setOrientation(LinearLayout.VERTICAL);

			this.addView(mTop);
			this.addView(mContent);
		}

		/**
		 * 添加标题
		 */
		public void addTitle(String mText) {
			TextView mTitle = new TextView(getContext()){
				@Override  
				public boolean isFocused() {  
				      return true;  
				} 
			};
			mTitle .setEllipsize(TruncateAt.MARQUEE);
			mTitle .setSingleLine(true);
			mTitle .setMarqueeRepeatLimit(6);
			mTitle.setText(mText);
			mTitle.setTextColor(Color.BLACK);
			mTitle.setTextSize(24);
//			mTitle.setShadowLayer(2, 0, 0, Color.BLACK);
			mTitle.setLines(1);
			mTop.addView(mTitle);
		}

		/**
		 * 输入框
		 */
		public void addEditText(String mHeader, String mHint, String mText) {
			LinearLayout mLayout = new LinearLayout(getContext());
			mLayout.setTag("EditText");
			mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLayout.setGravity(Gravity.CENTER_VERTICAL);
			mLayout.setPadding(0, Utils.dipTopx(getContext(), 7), 0, 0);

			TextView mTextView = new TextView(getContext());
			mTextView.setPadding(0, 0, Utils.dipTopx(getContext(), 13), 0);
			mTextView.setText(mHeader);
			mTextView.setTextColor(Color.WHITE);
			mTextView.setTextSize(16);
//			mTextView.setShadowLayer(2, 0, 0, Color.BLACK);

			final EditText et = new EditText(getContext());
			et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT, 1));
			et.setTextSize(16);
			et.setHint(mHint);
			if (mText != null) {
				et.setText(mText);
			}
			mLayout.addView(mTextView);
			mLayout.addView(et);
			mContent.addView(mLayout);
		}
		/**
		 * 输入框
		 */
		public void addEditText( String mText) {
			LinearLayout mLayout = new LinearLayout(getContext());
			mLayout.setTag("EditText");
			mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLayout.setGravity(Gravity.CENTER_VERTICAL);
			mLayout.setPadding(0, Utils.dipTopx(getContext(), 7), 0, 0);
			
//			mTextView.setShadowLayer(2, 0, 0, Color.BLACK);
			
			final EditText et = new EditText(getContext());
			et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT, 1));
			et.setTextSize(16);
			if (mText != null) {
				et.setText(mText);
			}
			mLayout.addView(et);
			mContent.addView(mLayout);
		}

		/**
		 * 文字
		 */
		public void addTextView(String mText) {
			TextView mTextView = new TextView(getContext());
			mTextView.setTag("TextView");
			mTextView.setPadding(Utils.dipTopx(getContext(), 13),
					Utils.dipTopx(getContext(), 7), 0, 0);
			mTextView.setText(mText);
			mTextView.setTextColor(Color.WHITE);
			mTextView.setTextSize(16);
//			mTextView.setShadowLayer(2, 0, 0, Color.BLACK);
			mContent.addView(mTextView);
		}

		/**
		 * 复选框
		 */
		public void addCheckBox(String mText,boolean checked,String tvText) {
			LinearLayout mLayout = new LinearLayout(getContext());
			mLayout.setOrientation(LinearLayout.VERTICAL);
			mLayout.setTag("CheckBox");
			mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLayout.setGravity(Gravity.CENTER_VERTICAL);
			mLayout.setPadding(Utils.dipTopx(getContext(), 13),
					Utils.dipTopx(getContext(), 7), 0, 0);

			CheckBox cb = new CheckBox(getContext());
			cb.setChecked(checked);
			cb.setText(mText);
			cb.setTextColor(Color.WHITE);
			cb.setTextSize(14);
//			cb.setShadowLayer(2, 0, 0, 0xfff18f08);
			
			mLayout.addView(cb);
			if(null!=tvText&&!"".equals(tvText)){
				TextView tv = new TextView(getContext());
				tv.setText(tvText);
				tv.setTextColor(Color.WHITE);
				tv.setTextSize(10);
				tv.setGravity(Gravity.TOP);
				tv.setPadding(Utils.dipTopx(getContext(), 36),0, 0, 0);
				mLayout.addView(tv);
			}
			mContent.addView(mLayout);
		}

		/**
		 * 单选框
		 */
		public void addRadioGroup(String[] group) {
			LinearLayout mLayout = new LinearLayout(getContext());
			mLayout.setTag("RadioGroup");
			mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLayout.setGravity(Gravity.CENTER_VERTICAL);
			mLayout.setPadding(Utils.dipTopx(getContext(), 13),
					Utils.dipTopx(getContext(), 7), 0, 0);

			RadioGroup radiogroup = new RadioGroup(getContext());
			for (int i = 0; i < group.length; i++) {
				RadioButton radio = new RadioButton(getContext());
				radio.setText(group[i]);
				radio.setTextColor(Color.WHITE);
				radio.setTextSize(16);
//				radio.setShadowLayer(2, 0, 0, Color.BLACK);
				radio.setId(i);
				radiogroup.addView(radio);
				if (i == 0) {
					radio.setChecked(true);
				}
			}
			mLayout.addView(radiogroup);
			mContent.addView(mLayout);
		}
		
		/**
		 * Added by LiYongNam 2012.12.13_start
		 */
		
		public void addNumberPicker( String[] items ){
			if ( items != null && items.length > 0 ){
				NumberPicker mNumberPicker = new NumberPicker( getContext() );			
				mNumberPicker.setTag( "NumberPicker" );
				mNumberPicker.setLayoutParams( new LayoutParams ( LayoutParams.FILL_PARENT,
						LayoutParams.WRAP_CONTENT ) );
				mNumberPicker.setRange( 1, items.length, items );
				mNumberPicker.setCurrent( 1 );
				
				mNumberPicker.setPadding(Utils.dipTopx(getContext(), 13),
						Utils.dipTopx(getContext(), 7), 0, 0);
				
				mContent.addView( mNumberPicker );
			}			
		}
		/**
		 * Added by LiYongNam 2012.12.13_end
		 */

		/**
		 * 按钮
		 */
		public void addEnterButton(String[] buttons,
				final EnterClickListener mListener) {

			LinearLayout mImageLayout = new LinearLayout(getContext());
			mImageLayout.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			mImageLayout.setPadding(0, Utils.dipTopx(getContext(), 5), 0, 0);

			ImageView mLine = new ImageView(getContext());
			mLine.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLine.setBackgroundResource(R.drawable.bg_transparent);

			mImageLayout.addView(mLine);
			mContent.addView(mImageLayout);

			LinearLayout mLayout = new LinearLayout(getContext());
			mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLayout.setPadding(0, Utils.dipTopx(getContext(), 7), 0,  Utils.dipTopx(getContext(), 7));
			mLayout.setGravity(Gravity.CENTER_VERTICAL);

			for (int i = 0; i < buttons.length; i++) {
				String text = buttons[i];

				LinearLayout mLayout2 = new LinearLayout(mLayout.getContext());
				mLayout2.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
				mLayout2.setGravity(Gravity.CENTER);

				final Button bt = new Button(mLayout.getContext());
				bt.setBackgroundResource(R.drawable.button_hover);
				bt.setWidth(width * 150 / 480);
				bt.setTag(i);
				bt.setText(text);
				bt.setTextSize(17);
				bt.setTextColor(Color.BLACK);
				bt.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						int count = mContent.getChildCount();
						ArrayList<String> mText = new ArrayList<String>();
						ArrayList<Boolean> mCheck = new ArrayList<Boolean>();
						ArrayList<Integer> mRadio = new ArrayList<Integer>();						

						for (int i = 0; i < count; i++) {
							Object obj = mContent.getChildAt(i).getTag();
							if (obj != null
									&& obj.toString().equals("EditText")) {
								LinearLayout mLayout = (LinearLayout) mContent
										.getChildAt(i);
								int position = mLayout.getChildCount()-1;
								EditText mEditText = (EditText) mLayout
										.getChildAt(position);
								mText.add("" + mEditText.getText());
							}
							if (obj != null
									&& obj.toString().equals("CheckBox")) {
								LinearLayout mLayout = (LinearLayout) mContent
										.getChildAt(i);
								CheckBox mCheckBox = (CheckBox) mLayout
										.getChildAt(0);
								mCheck.add(mCheckBox.isChecked());
							}
							if (obj != null
									&& obj.toString().equals("RadioGroup")) {
								LinearLayout mLayout = (LinearLayout) mContent
										.getChildAt(i);
								RadioGroup mRadioGroup = (RadioGroup) mLayout
										.getChildAt(0);
								mRadio.add(mRadioGroup
										.getCheckedRadioButtonId());
							}							
						}
						
						if (mListener != null)
							mListener.onClick(mText, mCheck, mRadio,
									Integer.parseInt(bt.getTag().toString()));
					}
				});
				mLayout2.addView(bt);
				mLayout.addView(mLayout2);
			}
			mContent.addView(mLayout);
		}
		
		public void addEnterButton_1(String[] buttons,
				final EnterClickListener_1 mListener) {

			LinearLayout mImageLayout = new LinearLayout(getContext());
			mImageLayout.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			mImageLayout.setPadding(0, Utils.dipTopx(getContext(), 5), 0, 0);

			ImageView mLine = new ImageView(getContext());
			mLine.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLine.setBackgroundResource(R.drawable.bg_transparent);

			mImageLayout.addView(mLine);
			mContent.addView(mImageLayout);

			LinearLayout mLayout = new LinearLayout(getContext());
			mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			mLayout.setPadding(0, Utils.dipTopx(getContext(), 7), 0,  com.vgooo.util.Utils.dipTopx(getContext(), 7));
			mLayout.setGravity(Gravity.CENTER_VERTICAL);

			for (int i = 0; i < buttons.length; i++) {
				String text = buttons[i];

				LinearLayout mLayout2 = new LinearLayout(mLayout.getContext());
				mLayout2.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
				mLayout2.setGravity(Gravity.CENTER);

				final Button bt = new Button(mLayout.getContext());
				bt.setBackgroundResource(R.drawable.button_hover);
				bt.setWidth(width * 150 / 480);
				bt.setTag(i);
				bt.setText(text);
				bt.setTextSize(17);
				bt.setTextColor(Color.BLACK);
				bt.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						int count = mContent.getChildCount();
						ArrayList<String> mText = new ArrayList<String>();
						ArrayList<Boolean> mCheck = new ArrayList<Boolean>();
						ArrayList<Integer> mRadio = new ArrayList<Integer>();
						ArrayList<String>  mNP    = new ArrayList<String>();                        

						for (int i = 0; i < count; i++) {
							Object obj = mContent.getChildAt(i).getTag();
							if (obj != null
									&& obj.toString().equals("EditText")) {
								LinearLayout mLayout = (LinearLayout) mContent
										.getChildAt(i);
								EditText mEditText = (EditText) mLayout
										.getChildAt(1);
								mText.add("" + mEditText.getText());
							}
							if (obj != null
									&& obj.toString().equals("CheckBox")) {
								LinearLayout mLayout = (LinearLayout) mContent
										.getChildAt(i);
								CheckBox mCheckBox = (CheckBox) mLayout
										.getChildAt(0);
								mCheck.add(mCheckBox.isChecked());
							}
							if (obj != null
									&& obj.toString().equals("RadioGroup")) {
								LinearLayout mLayout = (LinearLayout) mContent
										.getChildAt(i);
								RadioGroup mRadioGroup = (RadioGroup) mLayout
										.getChildAt(0);
								mRadio.add(mRadioGroup
										.getCheckedRadioButtonId());
							}
							/**
							 * Added by LiYongNam_3rd 2012.12.13_start
							 */
							if ( obj != null 
									&& obj.toString().equals("NumberPicker")) {
								NumberPicker mNumberPicker= ( NumberPicker ) mContent
										.getChildAt( i );
								mNP.add( mNumberPicker
										.getTextValue() );
							}
							/**
							 * Added by LiYongNam_3rd 2012.12.13_end
							 */
						}
						
						if (mListener != null)
							mListener.onClick(mText, mCheck, mRadio, mNP,
									Integer.parseInt(bt.getTag().toString()));
					}
				});
				mLayout2.addView(bt);
				mLayout.addView(mLayout2);
			}
			mContent.addView(mLayout);
		}
	}
	
	
}

